﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using ZG_member.Model;
using System.Configuration;
namespace ZG_member
{
    public partial class birthdayFrom : Form
    {

        public birthdayFrom()
        { 
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        BLL.birthdayremind br = new BLL.birthdayremind();
        private void button2_Click(object sender, EventArgs e)
        {
            Model.birthdayremind brmodl = br.GetModel(2);
            if (brmodl == null)
            {
                Model.birthdayremind brmodl1 = new Model.birthdayremind();
                brmodl1.Remindday = int.Parse(this.days.Text);
                brmodl1.Remindcontent = this.contextTxt.Text;
                brmodl1.Nowremindcontent1 = this.nowContent.Text;
                string shengrijifen = this.txtbeishu.Text ;
                brmodl1.Birthdaydiscount = shengrijifen;
                bool a = br.add(brmodl1);
                if (a)
                {
                    MessageBox.Show("添加成功！");
                    this.Close();
                    return;
                }
            }
            else
            {
                brmodl.Remindday = int.Parse(this.days.Text);
                brmodl.Remindcontent = this.contextTxt.Text;
                brmodl.Nowremindcontent1 = this.nowContent.Text;
                string shengrijifen = this.txtbeishu.Text ;
                brmodl.Birthdaydiscount = shengrijifen;
                bool a = br.Update(brmodl);
                if (a)
                {
                    MessageBox.Show("修改成功！");

                    return;
                }
            }
            
        }

        private void birthdayFrom_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;
            this.timer1.Start();
            Model.birthdayremind brmodl = br.GetModel(2);
            if (brmodl==null)
            {
                MessageBox.Show("您还没有设置任何提醒！");
                return;
            }
            this.days.Text = brmodl.Remindday.ToString();
            this.contextTxt.Text = brmodl.Remindcontent;
            this.nowContent.Text = brmodl.Nowremindcontent1;
            string shengrijifen = brmodl.Birthdaydiscount;
            this.txtbeishu.Text = shengrijifen;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Model.birthdayremind brmodl = br.GetModel(2);
            if (brmodl == null)
            {
                Model.birthdayremind brmodl1 = new Model.birthdayremind();
                brmodl1.Remindday = int.Parse(this.days.Text);
                brmodl1.Remindcontent = this.contextTxt.Text;
                brmodl1.Nowremindcontent1 = this.nowContent.Text;
                string shengrijifen = this.txtbeishu.Text ;
                brmodl1.Birthdaydiscount = shengrijifen;
                bool a = br.add(brmodl1);
                if (a)
                {
                    MessageBox.Show("添加成功！");
                    this.Close();
                    return;
                }
            }
            else
            {
                brmodl.Remindday = int.Parse(this.days.Text);
                brmodl.Remindcontent = this.contextTxt.Text;
                brmodl.Nowremindcontent1 = this.nowContent.Text;
                string shengrijifen = this.txtbeishu.Text;
                brmodl.Birthdaydiscount = shengrijifen;
                bool a = br.Update(brmodl);
                if (a)
                {
                    MessageBox.Show("修改成功！");
                    this.Close();
                    return;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void birthdayFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void discountTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void days_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }
    }
}
